import { Component, OnInit } from '@angular/core';
import { ApiService } from '../../../../providers/ApiPro.service';
import { NzModalService } from 'ng-zorro-antd';
import { ActivatedRoute, Router, RouterLink, RouterModule } from '@angular/router';
import { HybBigCustomerAddDto } from '../HuoMesstmp.model';

import {
    FormBuilder,
    FormGroup,
    FormControl,
    Validators
} from '@angular/forms';

@Component({
  selector: 'app-MesshuoyunAdminDialog',
  templateUrl: './MesshuoyunAdminDialog.component.html',
  styleUrls: ['./MesshuoyunAdminDialog.component.scss']
})
export class MesshuoyunAdminDialogComponent implements OnInit {
  public validateForm: FormGroup;
  public cusDto: HybBigCustomerAddDto = new HybBigCustomerAddDto();
  public token = {
     Authorization: null
   };
  public ischeck: boolean = false;
  public isEdit: boolean = false;
  public isAdd: boolean = false;
  public submitFlg: boolean = true;
  public  fileList = [];

    _submitForm() {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[ i ].markAsDirty();
        if( i != 'logisticsCompanyId'){
          if(this.validateForm.value[ i ] == null || this.validateForm.value[ i ] ==''){
            this.submitFlg = false;
          }
        }
        }
      console.log(this.validateForm.value)
    }

  constructor(
    private fb: FormBuilder,
    private apiService: ApiService,
    private router: Router,
    private route: ActivatedRoute,
    private confirmServ: NzModalService
) { }

  ngOnInit() {
    this.getToken();
    this.getId();
  }

  initForm(){
     this.validateForm = this.fb.group({
      logisticsCompanyId : [ null ],
      logisticsCompanyName : [ null, [ Validators.required ] ],
      checkInTime : [ null, [ Validators.required ] ],
      contacts : [ null, [ Validators.required ] ],
      contactsMobile : [ null, [ Validators.required ] ],
      legalPersonId : [ null, [ Validators.required ] ],
      bankName : [ null, [ Validators.required ] ],
      bankCardNo : [ null, [ Validators.required ] ],
      driverNum : [ null, [ Validators.required ] ],
      carNum : [ null, [ Validators.required ] ]
    });
  }

  getId(){
     this.isEdit = false;
     this.isAdd = false;
     this.route.params.subscribe((params) => {
       console.log(params['id']);
       console.log(params['edit']);
       if((params['edit']) == "edit" ){
        this.isEdit = true;
       }else{
        this.isEdit = false;
       }
            if((params['id'])){
              this.isAdd = false;
              this.load(params['id']);
           }else{
            this.isAdd = true;
             this.initForm();
           }
        });

  }

  getToken(){
    this.token.Authorization = "Bearer "+this.apiService.getLocalStroage();
  }

  load(id) {
    this.initForm();
    let url = "findlogisticscompanydetail";
    this.apiService.get(url, { "logisticsCompanyId": id })
      .then(res => {
        this.validateForm = this.fb.group({
          logisticsCompanyId    : [ res.result.logisticsCompanyId ,],
          logisticsCompanyName  : [ res.result.logisticsCompanyName, [ Validators.required ] ],
          checkInTime           : [ res.result.checkInTime, [ Validators.required ] ],
          contacts              : [ res.result.contacts, [ Validators.required ] ],
          contactsMobile        : [ res.result.contactsMobile , [ Validators.required ] ],
          legalPersonId         : [ res.result.legalPersonId, [ Validators.required ] ],
          bankName              : [ res.result.bankName, [ Validators.required ] ],
          bankCardNo            : [ res.result.bankCardNo, [ Validators.required ] ],
          driverNum             : [ res.result.driverNum, [ Validators.required ] ],
          carNum                : [ res.result.carNum, [ Validators.required ] ]
        });
        console.log(this.validateForm.value);
      })
  }

  modify(){
    // console.log(this.validateForm.value);
    // console.log(this.isEdit);
  }

  getFormControl(name) {
    return this.validateForm.controls[ name ];
  }

  createNew(){
    this.submitFlg = true;
    this._submitForm();
    let url = "addlogistisccompany";
    let url2 = "eidtlogistisccompany";
    
    if(this.submitFlg){

      let date1 = this.validateForm.value.checkInTime + '0'
      if (date1.indexOf('-')>-1){
        console.log(date1);
      }else{
        this.validateForm.value.checkInTime=this.apiService.formatDateTime(this.validateForm.value.checkInTime);
      }
      
      if(this.isEdit){
        this.apiService.post(url2,this.validateForm.value)
           .then(res =>{
          console.log(res);
          if(res.result){
            this.success();          
          }else{
            this.error(res.errMsg);
          }
        })
        .catch(err =>{
          console.log(err);
          this.error(err.errMsg);
        })
      }else{
        this.apiService.post(url,this.validateForm.value)
        .then(res =>{
          console.log(res);
          if(res.result){
            this.success();          
          }else{
            this.error(res.errMsg);
          }
        })
        .catch(err =>{
          console.log(err);
          this.error(err.errMsg);
        })
      }
    }
    
  }

  error(err) {
    this.confirmServ.error({
      title: '错误',
      content: err,
      okText:'返回'
    });
  }

  success(){
    let self = this;
      this.confirmServ.success({
      title: '成功',
      content: '即将返回货物承运管理页面',
      okText:'返回',
      onOk() {
        self.router.navigateByUrl("layout/mess/huoyun");
      },
    });
  }

}
